
import commands
from tool.OutputResult import OutputResult
import csv 

def convert_to_3f(val):
    val=str(val)
    if len(val)==1:
        val="U00"+val
    if len(val)==2:
        val="U0"+val
    if len(val)==3:
        val="U"+val
    return val


def createData(featureFile, dataFile, index):
    fileIn=open(featureFile)
    fileOut=open(dataFile,'w')
    count=0
    for line in fileIn:
        spl=line.strip().split(",")
        count+=1
        if spl[len(spl)-index]=="1" and count>1:
            fileOut.write("\n")
        fileOut.write(spl[len(spl)-index]+"\t"+"\t".join(spl)+"\n")
    fileOut.close()
    fileIn.close()
    
def createTemplate(trainDataFile):
    fileIn=open(trainDataFile)
    fileOut=open("../../data/template",'w')
    count=0
    line=fileIn.readline()
    spl=line.strip().split("\t")
    #print spl
    features=len(spl)
    fileOut.write("# Unigram\n")
    for i in range(0, features-1):
        #U26:%x[0,26]/%x[0,29]
        #fileOut.write(convert_to_3f(i)+":%x[0,"+str(i)+"]/%x[0,"+str(features-1)+"]\n")
        fileOut.write(convert_to_3f(i)+":%x[0,"+str(i)+"]\n")#/%x[0,"+str(features-1)+"]\n")
    
    #To check bigram features!
    fileOut.write("\n\n# Bigram\nB\n\n\n")
    fileOut.write("#indirect features\n")
    fileOut.write(convert_to_3f(features-1)+":%x[-1,"+str(features-2)+"]\n")
    fileOut.write(convert_to_3f(features)+":%x[-1,"+str(features-3)+"]\n")
    fileOut.write(convert_to_3f(features+1)+":%x[-1,"+str(features-4)+"]\n")
    fileOut.write(convert_to_3f(features-1)+":%x[-2,"+str(features-2)+"]\n")
    fileOut.write(convert_to_3f(features)+":%x[-2,"+str(features-3)+"]\n")
    fileOut.write(convert_to_3f(features+1)+":%x[-2,"+str(features-4)+"]\n")
    
def getFormattedOutput(finalResultsFile, features):
    fileInp= open("../../data/tempOut.txt")
    fileOut=open(finalResultsFile,'w')
    for line in fileInp:
        spl=line.strip().split("\t")
        if len(spl)>100: #==features
            fileOut.write(",".join(spl[1:])+ "\n")
    fileOut.close()
    fileInp.close()

def getFormattedOutputNew(finalResultsFile, features):
    fileInp= open("../../data/tempOut.txt")
    fileOut=open(finalResultsFile,'wb')
    finalResul = []
    for line in fileInp:
        spl=line.strip().split("\t")
        if len(spl)>100: #==features
            # spl contain needed information 
            final = {'1':0,'2':0,'3':0,'4':0,'5':0,'6':0}
            for item in spl:
                if ('/' in item):
                    # Split the value 
                    key = item.split("/")[0]
                    value  = float(item.split("/")[1])
                    final[key] = value
            # Write it to the test file
            finalResul.append(final) 
    # Write to output 
    testFile = open("../../data/test.csv")
    spamReader = csv.reader(testFile,delimiter = ",")
    spamWriter = csv.writer(fileOut, delimiter = ",")
    count = 0
    
    output = OutputResult()
    for line in spamReader:
        count+=1 
        if (count ==1):
            line = line + ["Prediction"]
        else:
            t1 = (count-2) / 6 
            label = line[0]
            key = 5
            if (label == "population"): key = 1
            if (label == "intervention"): key = 2 
            if (label == "background"): key = 3
            if (label == "outcome"): key = 4
            if (label == "study design"): key = 5
            if (label == "other"): key = 6
            prob = finalResul[t1][str(key)]         
            line  = line + [str(output.f(prob,5))]  
        spamWriter.writerow(line)
    
    testFile.close()    
    fileOut.close()
    fileInp.close()
    
def running():
    createData("../../data/lemmaFeaturesTrain_filtered.csv", "../../data/train.data", 4)
    createData("../../data/lemmaFeaturesTest_filtered.csv", "../../data/test.data", 3)
    createTemplate("../../data/train.data")
    
    commands.getoutput("../../CRF++-0.57/crf_learn -a -p 2 -f 10 -c 0.065 ../../data/template ../../data/train.data ../../data/model")
    commands.getoutput("../../CRF++-0.57/crf_test -v2 -m ../../data/model ../../data/test.data > ../../data/tempOut.txt")
    #getFormattedOutput("../../data/CRFOutput_Number.csv", 100)
    getFormattedOutputNew("../../data/crf_700_dependces_newTrain_filtered.csv", 100)
    # OUTPUT RESlULT
     
    #output = OutputResult()
    #inputFile = "../../data/CRFOutput_Number.csv"
    #result = output.ConvertFromNumberToLabels(inputFile)
    
    #resultFile = "../../data/crf_500_newTrain.csv"
    #output.outputBaseOnSentenceLevelLabels(resultFile,result)
    
running()